package com.insight.common.mq.subscribe.cmp;

import com.yomahub.liteflow.annotation.LiteflowCmpDefine;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.annotation.LiteflowMethod;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.enums.LiteFlowMethodEnum;
import com.insight.common.mq.RedisMq;
import com.insight.common.mq.subscribe.context.SubscribeMessageContext;
import com.insight.common.mq.subscribe.entity.SubscribeMessage;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @author JinPengDong
 * @date 2023/12/19
 */
@LiteflowComponent
@LiteflowCmpDefine
public class CmpSubscribeMessage {
    @Autowired
    private RedisMq redisMq;

    @LiteflowMethod(LiteFlowMethodEnum.PROCESS)
    public void process(NodeComponent cmp) {
        final SubscribeMessageContext contextBean = cmp.getContextBean(SubscribeMessageContext.class);
        final String tag = cmp.getTag();
        final Map<String, SubscribeMessage> message = contextBean.getMessage();
        SubscribeMessage subscribeMessage = message.get(tag);
        if (subscribeMessage == null) {
            return;
        }
        redisMq.subscribe(subscribeMessage.getListener(), subscribeMessage.getStreams());
    }

}
